package net.goutalk.glcs.module.erpModel.caseErpMaterial.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.enums.YesOrNoEnum;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.RedisUtil;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.erpModel.caseErpLog.entity.CaseErpLog;
import net.goutalk.glcs.module.erpModel.caseErpLog.service.ICaseErpLogService;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.dto.*;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.entity.CaseErpMaterial;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.entity.CaseErpMaterialClasses;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.entity.CaseErpMaterialProperty;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.service.ICaseErpMaterialClassesService;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.service.ICaseErpMaterialPropertyService;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.service.ICaseErpMaterialService;
import net.goutalk.glcs.module.erpModel.caseErpMaterial.vo.*;
import net.goutalk.glcs.module.erpModel.caseErpPurchase.entity.CaseErpPurchase;
import net.goutalk.glcs.module.erpModel.caseErpPurchase.entity.CaseErpPurchaseDetail;
import net.goutalk.glcs.module.erpModel.caseErpPurchase.service.ICaseErpPurchaseService;
import net.goutalk.glcs.module.erpModel.caseErpStock.service.ICaseErpInstoreLogService;
import net.goutalk.glcs.module.erpModel.caseErpStock.service.ICaseErpOutstoreLogService;
import net.goutalk.glcs.module.erpModel.caseErpUnit.entity.CaseErpUnit;
import net.goutalk.glcs.module.erpModel.caseErpUnit.service.ICaseErpUnitService;
import net.goutalk.glcs.module.organization.entity.Department;
import net.goutalk.glcs.module.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 物料信息【case_erp_material】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-06-19
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_MATERIAL +"/caseErpMaterial")
@Api(value = GlobalConstant.CASE_ERP_MATERIAL +"/caseErpMaterial", tags = "物料清单")
@AllArgsConstructor
public class CaseErpMaterialController {
    private ICaseErpMaterialService caseErpMaterialService;

    private ICaseErpInstoreLogService caseErpInstoreLogService;

    private ICaseErpOutstoreLogService caseErpOutstoreLogService;

    private ICaseErpLogService caseErpLogService;

    private ICaseErpPurchaseService caseErpPurchaseService;

    private RedisUtil redisUtil;

    private ICaseErpUnitService caseErpUnitService;

    private ICaseErpMaterialClassesService caseErpMaterialClassesService;

    private ICaseErpMaterialPropertyService caseErpMaterialPropertyService;


    @GetMapping(value = "/list")
    @ApiOperation("物料清单列表")
    public R list(@RequestParam(required = false) String keyword) {
        List<CaseErpMaterialVo> list = caseErpMaterialService.selectJoinList(CaseErpMaterialVo.class,
                MPJWrappers.<CaseErpMaterial>lambdaJoin()
                        .distinct()
                        .like(StrUtil.isNotBlank(keyword),CaseErpMaterial::getName,keyword)
                        .selectAs(CaseErpUnit::getName, CaseErpMaterialVo::getUnitName)
                        .selectAs(CaseErpMaterialClasses::getTypeName, CaseErpMaterialVo::getTypeName)
                        .selectAs(CaseErpMaterialProperty::getPropertyName, CaseErpMaterialVo::getPropertyName)
                        .select(CaseErpMaterial::getId)
                        .select(CaseErpMaterial.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpMaterialVo.class).contains(x.getProperty()))
                        .leftJoin(CaseErpUnit.class,CaseErpUnit::getId,CaseErpMaterial::getUnitId)
                        .leftJoin(CaseErpMaterialClasses.class,CaseErpMaterialClasses::getId,CaseErpMaterial::getClassesId)
                        .leftJoin(CaseErpMaterialProperty.class,CaseErpMaterialProperty::getId,CaseErpMaterial::getPropertyId));

        return R.ok(list);
    }

    @GetMapping(value = "/page")
    @ApiOperation("物料清单列表分页")
    public R page(CaseErpMaterialPageDto dto) {
        IPage<CaseErpMaterialVo> page = caseErpMaterialService.selectJoinListPage(ConventPage.getPage(dto), CaseErpMaterialVo.class,
                MPJWrappers.<CaseErpMaterial>lambdaJoin()
                        .distinct()
                        .orderByDesc(CaseErpMaterial::getCreateDate)
                        .like(StrUtil.isNotBlank(dto.getName()), CaseErpMaterial::getName, dto.getName())
                        .eq(ObjectUtil.isNotEmpty(dto.getClassesId()),CaseErpMaterial::getClassesId,dto.getClassesId())
                        .eq(ObjectUtil.isNotEmpty(dto.getPropertyId()),CaseErpMaterial::getPropertyId,dto.getPropertyId())
                        .eq(ObjectUtil.isNotEmpty(dto.getState()),CaseErpMaterial::getState,dto.getState())
                        .selectAs(CaseErpUnit::getName, CaseErpMaterialVo::getUnitName)
                        .selectAs(CaseErpMaterialClasses::getTypeName, CaseErpMaterialVo::getTypeName)
                        .selectAs(CaseErpMaterialProperty::getPropertyName, CaseErpMaterialVo::getPropertyName)
                        .select(CaseErpMaterial::getId)
                        .select(CaseErpMaterial.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpMaterialVo.class).contains(x.getProperty()))
                        .leftJoin(CaseErpUnit.class,CaseErpUnit::getId,CaseErpMaterial::getUnitId)
                        .leftJoin(CaseErpMaterialClasses.class,CaseErpMaterialClasses::getId,CaseErpMaterial::getClassesId)
                        .leftJoin(CaseErpMaterialProperty.class,CaseErpMaterialProperty::getId,CaseErpMaterial::getPropertyId));

        PageOutput<CaseErpMaterialVo> pageOutput = ConventPage.getPageOutput(page);
        return R.ok(pageOutput);
    }


    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询物料信息")
    public R info(@RequestParam Long id) {
        CaseErpMaterialInfoVo caseErpMaterialInfoVo = caseErpMaterialService.selectJoinOne(CaseErpMaterialInfoVo.class,
                MPJWrappers.<CaseErpMaterial>lambdaJoin()
                        .distinct()
                        .eq(CaseErpMaterial::getId,id)
                        .selectAs(CaseErpUnit::getName, CaseErpMaterialInfoVo::getUnitName)
                        .selectAs(CaseErpMaterialClasses::getTypeName, CaseErpMaterialInfoVo::getTypeName)
                        .selectAs(CaseErpMaterialProperty::getPropertyName, CaseErpMaterialInfoVo::getPropertyName)
                        .select(CaseErpMaterial::getId)
                        .select(CaseErpMaterial.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpMaterialInfoVo.class).contains(x.getProperty()))
                        .leftJoin(CaseErpUnit.class,CaseErpUnit::getId,CaseErpMaterial::getUnitId)
                        .leftJoin(CaseErpMaterialClasses.class,CaseErpMaterialClasses::getId,CaseErpMaterial::getClassesId)
                        .leftJoin(CaseErpMaterialProperty.class,CaseErpMaterialProperty::getId,CaseErpMaterial::getPropertyId));

        if (ObjectUtil.isNotEmpty(caseErpMaterialInfoVo.getIsSysCode()) && caseErpMaterialInfoVo.getIsSysCode() == YesOrNoEnum.YES.getCode()){
            caseErpMaterialInfoVo.setIsSysCodeBoolean(true);
        }else {
            caseErpMaterialInfoVo.setIsSysCodeBoolean(false);
        }
        if (caseErpMaterialInfoVo == null) {
            R.error("找不到此物料清单！");
        }
        List<User> userList = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        if (ObjectUtil.isNotEmpty(caseErpMaterialInfoVo.getCreateDate())){
            caseErpMaterialInfoVo.setCreateDate(caseErpMaterialInfoVo.getCreateDate());
            User user = userList.stream().filter(x -> x.getId().equals(caseErpMaterialInfoVo.getCreateUserId())).findFirst().orElse(new User());
            caseErpMaterialInfoVo.setCreateUserName(user.getName());
        }
        if(ObjectUtil.isNotEmpty(caseErpMaterialInfoVo.getModifyDate())){
            caseErpMaterialInfoVo.setModifyDate(caseErpMaterialInfoVo.getModifyDate());
            User user = userList.stream().filter(x -> x.getId().equals(caseErpMaterialInfoVo.getModifyUserId())).findFirst().orElse(new User());
            caseErpMaterialInfoVo.setModifyUserName(user.getName());
        }
        caseErpMaterialInfoVo.setAHouse(12);
        caseErpMaterialInfoVo.setLocalHouse(20);
        caseErpMaterialInfoVo.setResearchHouse(3);
        caseErpMaterialInfoVo.setRawMaterialHouse(12);
        caseErpMaterialInfoVo.setTotalHouse(47);
        return R.ok(caseErpMaterialInfoVo);
    }

    @PostMapping
    @ApiOperation(value = "新增物料信息")
    public R add(@Valid @RequestBody AddCaseErpMaterialDto dto) {
        long count = caseErpMaterialService.count(Wrappers.<CaseErpMaterial>query().lambda().eq(CaseErpMaterial::getName, dto.getName()));
        if (count > 0) {
            return R.error("此物料已经存在！");
        }

        CaseErpMaterial  caseErpMaterial= BeanUtil.toBean(dto, CaseErpMaterial.class);
        if (dto.getIsSysCodeBoolean()){
            caseErpMaterial.setIsSysCode(YesOrNoEnum.YES.getCode());
        }else {
            caseErpMaterial.setIsSysCode(YesOrNoEnum.NO.getCode());
        }
        caseErpMaterial.setState(YesOrNoEnum.YES.getCode());
        caseErpMaterial.setInventory(BigDecimal.valueOf(20));
        caseErpMaterialService.save(caseErpMaterial);

        CaseErpLog log = new CaseErpLog();
        log.setKeyId(caseErpMaterial.getId());

        log.setCategoryId(1);
        log.setOperateTime(LocalDateTime.now());
        User user = StpUtil.getTokenSession().get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
        log.setOperateUserAccount(user.getName());
        log.setExecuteResultJson("新增物料信息");
        caseErpLogService.save(log);

//        CaseErpInstoreLog instoreLog = new CaseErpInstoreLog();
//        instoreLog.setMaterialId(caseErpMaterial.getId());
//        instoreLog.setTheme("201来料入库");
//        instoreLog.setCode(caseErpMaterial.getCode());
//        instoreLog.setDate(LocalDateTime.now());
//        instoreLog.setInsertType("采购入库");
//        instoreLog.setCount(new BigDecimal("100"));
//        instoreLog.setPerson("张成");
//        instoreLog.setStore("原料库A01");
//        caseErpInstoreLogService.save(instoreLog);
//
//        CaseErpOutstoreLog outstoreLog = new CaseErpOutstoreLog();
//        outstoreLog.setMaterialId(caseErpMaterial.getId());
//        outstoreLog.setTheme("201来料出库");
//        outstoreLog.setCode(caseErpMaterial.getCode());
//        outstoreLog.setDate(LocalDateTime.now());
//        outstoreLog.setOutType("内部领用");
//        outstoreLog.setCount(new BigDecimal("50"));
//        outstoreLog.setPerson("张成");
//        outstoreLog.setStore("原料库A01");
//        caseErpOutstoreLogService.save(outstoreLog);

        return R.ok(true);
    }

    @PutMapping
    @ApiOperation(value = "修改物料信息")
    public R update(@Valid @RequestBody UpdateCaseErpMaterialDto dto) {
        CaseErpMaterial caseErpMaterial = BeanUtil.toBean(dto, CaseErpMaterial.class);
        if (dto.getIsSysCodeBoolean()){
            caseErpMaterial.setIsSysCode(YesOrNoEnum.YES.getCode());
        }else {
            caseErpMaterial.setIsSysCode(YesOrNoEnum.NO.getCode());
        }
        caseErpMaterial.setState(YesOrNoEnum.YES.getCode());
        caseErpMaterialService.updateById(caseErpMaterial);

        CaseErpLog log = new CaseErpLog();
        log.setKeyId(dto.getId());
        log.setCategoryId(1);
        log.setOperateTime(LocalDateTime.now());
        User user = StpUtil.getTokenSession().get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
        log.setOperateUserAccount(user.getName());
        log.setExecuteResultJson("编辑物料信息");
        caseErpLogService.save(log);

        return R.ok(true);
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除物料信息")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpMaterialService.removeById(id));
    }

    @GetMapping("/codeNumber")
    @ApiOperation(value = "获取物料编码")
    public R getCodeNumber(){
        return R.ok(caseErpMaterialService.getCodeNumber());
    }

    @PostMapping(value = "/export")
    @ApiOperation(value = "导出")
    @GlcsLog(value = "导出表单数据")
    public ResponseEntity<byte[]> export(@Valid @RequestBody List<Long> ids) {
        List<CaseErpMaterialExportVo> caseErpMaterialExportVoList= caseErpMaterialService.selectJoinList(CaseErpMaterialExportVo.class,
                MPJWrappers.<CaseErpMaterial>lambdaJoin()
                        .distinct()
                        .in(CaseErpMaterial::getId, ids)
                        .selectAs(CaseErpUnit::getName, CaseErpMaterialExportVo::getUnitName)
                        .selectAs(CaseErpMaterialClasses::getTypeName, CaseErpMaterialExportVo::getTypeName)
                        .selectAs(CaseErpMaterialProperty::getPropertyName, CaseErpMaterialExportVo::getPropertyName)
                        .select(CaseErpMaterial::getId)
                        .select(CaseErpMaterial.class, x -> VoToColumnUtil.fieldsToColumns(CaseErpMaterialExportVo.class).contains(x.getProperty()))
                        .leftJoin(CaseErpUnit.class,CaseErpUnit::getId,CaseErpMaterial::getUnitId)
                        .leftJoin(CaseErpMaterialClasses.class,CaseErpMaterialClasses::getId,CaseErpMaterial::getClassesId)
                        .leftJoin(CaseErpMaterialProperty.class,CaseErpMaterialProperty::getId,CaseErpMaterial::getPropertyId));
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, CaseErpMaterialExportVo.class).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(caseErpMaterialExportVoList);
        return R.fileStream(bot.toByteArray(), "物料清单" + ExcelTypeEnum.XLSX.getValue());
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public R importData(@RequestParam MultipartFile file) throws IOException {
        List<CaseErpUnit> caseErpUnitList = caseErpUnitService.list();
        List<CaseErpMaterialClasses> caseErpMaterialClassesList = caseErpMaterialClassesService.list();
        List<CaseErpMaterialProperty> caseErpMaterialPropertyList = caseErpMaterialPropertyService.list();
        List<ImportCaseErpMaterialDto> savedDataList = EasyExcel.read(file.getInputStream()).head(ImportCaseErpMaterialDto.class).sheet().doReadSync();
        List<AddCaseErpMaterialDto> addCaseErpMaterialDtoList = new ArrayList<>();
        for (ImportCaseErpMaterialDto importCaseErpMaterialDto : savedDataList) {
            AddCaseErpMaterialDto addCaseErpMaterialDto = BeanUtil.toBean(importCaseErpMaterialDto,AddCaseErpMaterialDto.class);
            CaseErpUnit caseErpUnit = caseErpUnitList.stream().filter(x -> x.getName().equals(importCaseErpMaterialDto.getUnitName())).findFirst().orElse(new CaseErpUnit());
            if (ObjectUtil.isNotEmpty(caseErpUnit.getId())){
                addCaseErpMaterialDto.setUnitId(caseErpUnit.getId());
            }else {
                R.error("找不到此单位");
            }
            CaseErpMaterialClasses caseErpMaterialClasses = caseErpMaterialClassesList.stream().filter(x -> x.getTypeName().equals(importCaseErpMaterialDto.getClassesName())).findFirst().orElse(new CaseErpMaterialClasses());
            if (ObjectUtil.isNotEmpty(caseErpMaterialClasses.getId())){
                addCaseErpMaterialDto.setClassesId(caseErpMaterialClasses.getId());
            }else {
                R.error("找不到此物料类型");
            }
            CaseErpMaterialProperty caseErpMaterialProperty = caseErpMaterialPropertyList.stream().filter(x -> x.getPropertyName().equals(importCaseErpMaterialDto.getPropertyName())).findFirst().orElse(new CaseErpMaterialProperty());
            if (ObjectUtil.isNotEmpty(caseErpMaterialProperty.getId())){
                addCaseErpMaterialDto.setPropertyId(caseErpMaterialProperty.getId());
            }else {
                R.error("找不到此物料属性");
            }
            addCaseErpMaterialDto.setInventory(BigDecimal.valueOf(20));
            addCaseErpMaterialDto.setState(YesOrNoEnum.YES.getCode());
            addCaseErpMaterialDtoList.add(addCaseErpMaterialDto);
        }
        caseErpMaterialService.saveBatch(BeanUtil.copyToList(addCaseErpMaterialDtoList, CaseErpMaterial.class));
        return R.ok(true);
    }

    @GetMapping("/export")
    @ApiOperation(value = "下载模板")
    @SneakyThrows
    public ResponseEntity<byte[]> export() {
        List<ImportCaseErpMaterialDto> caseErpMaterialList = new ArrayList<>();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, ImportCaseErpMaterialDto.class).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(BeanUtil.copyToList(caseErpMaterialList, ImportCaseErpMaterialDto.class));
        return R.fileStream(bot.toByteArray(), "物料清单模板" + ExcelTypeEnum.XLSX.getValue());
    }

    @GetMapping("/in-store")
    @ApiOperation("根据物料id查看入库详情")
    public R getInStoreById(@RequestParam Long id,@RequestParam(required = false) String keyword){
        CaseErpMaterial caseErpMaterial = caseErpMaterialService.getById(id);
        List<CaseErpInstoreLogVo> inStoreLogVos = new ArrayList<>();
        if (StrUtil.isNotBlank(caseErpMaterial.getCode())){
           inStoreLogVos = caseErpMaterialService.checkMaterialInstore(caseErpMaterial.getCode());
        }
        if (StrUtil.isNotBlank(keyword)){
            inStoreLogVos = inStoreLogVos.stream().filter(x->StrUtil.isNotBlank(x.getNumber()) && x.getNumber().contains(keyword)
                    || StrUtil.isNotBlank(x.getTheme()) && x.getTheme().contains(keyword)).collect(Collectors.toList());
        }
        return R.ok(inStoreLogVos);
    }

    @GetMapping("/out-store")
    @ApiOperation("根据物料id查看出库详情")
    public R getOutStoreById(@RequestParam Long id,@RequestParam(required = false) String keyword){
        CaseErpMaterial caseErpMaterial = caseErpMaterialService.getById(id);
        List<CaseErpOutstoreLogVo> outStoreLogVos = new ArrayList<>();
        if (StrUtil.isNotBlank(caseErpMaterial.getCode())){
            outStoreLogVos = caseErpMaterialService.checkMaterialOutStore(caseErpMaterial.getCode());
        }
        if (StrUtil.isNotBlank(keyword)){
            outStoreLogVos = outStoreLogVos.stream().filter(x->StrUtil.isNotBlank(x.getNumber()) && x.getNumber().contains(keyword)
                    || StrUtil.isNotBlank(x.getTheme()) && x.getTheme().contains(keyword)).collect(Collectors.toList());
        }

        return R.ok(outStoreLogVos);
    }

    @GetMapping("/log")
    @ApiOperation("根据物料id查看操作记录")
    public R getLogById(@RequestParam Long id,@RequestParam(required = false) String keyword){
        List<CaseErpLog> logVoList = caseErpLogService.list(Wrappers.<CaseErpLog>query().lambda()
                .eq(CaseErpLog::getKeyId, id)
                .like(StrUtil.isNotBlank(keyword),CaseErpLog::getOperateUserAccount,keyword)
                .or()
                .eq(CaseErpLog::getKeyId, id)
                .like(StrUtil.isNotBlank(keyword),CaseErpLog::getExecuteResultJson,keyword));
        return R.ok(logVoList);
    }

    @GetMapping("/purchase")
    @ApiOperation("根据物料id查看采购详情")
    public R getPurchaseById(@RequestParam Long id,@RequestParam(required = false) String keyword){
        List<PurchaseVo> vo= caseErpPurchaseService.selectJoinList(PurchaseVo.class,
                MPJWrappers.<CaseErpPurchase>lambdaJoin()
                        .distinct()
                        .eq(CaseErpMaterial::getId, id)
                        .like(StrUtil.isNotBlank(keyword),CaseErpPurchase::getTheme,keyword)
                        .selectAs(CaseErpPurchaseDetail::getCount, PurchaseVo::getCount)
                        .selectAs(CaseErpPurchaseDetail::getAfterTaxAmount, PurchaseVo::getAfterTaxAmount)
                        .selectAs(User::getName,PurchaseVo::getPurchasePersonName)
                        .selectAs(Department::getName,PurchaseVo::getPurchaseDeptName)
                        .selectAs(CaseErpPurchase::getPurchaseNumber,PurchaseVo::getNumber)
                        .select(CaseErpPurchase::getId)
                        .select(CaseErpPurchase.class, x -> VoToColumnUtil.fieldsToColumns(PurchaseVo.class).contains(x.getProperty()))
                        .leftJoin(CaseErpPurchaseDetail.class,CaseErpPurchaseDetail::getPurchaseId,CaseErpPurchase::getId)
                        .leftJoin(User.class,User::getId,CaseErpPurchase::getPurchasePersonId)
                        .leftJoin(Department.class,Department::getId,CaseErpPurchase::getPurchaseDeptId)
                        .leftJoin(CaseErpMaterial.class,CaseErpMaterial::getCode,CaseErpPurchaseDetail::getCode));

        for (PurchaseVo purchaseVo : vo) {
            if(purchaseVo.getAfterTaxAmount()!=null){
                purchaseVo.setAmountSum(purchaseVo.getAfterTaxAmount());
            }else {
                purchaseVo.setAmountSum(new BigDecimal("0"));
            }
        }
        return R.ok(vo);
    }


    @PutMapping("/stock-count")
    @ApiOperation(value = "库存盘点、报废，操作类型（-1：减少、1：增加、0：报废）")
    public R stockCount(@Valid @RequestBody StockCountDto dto) {
        return R.ok(caseErpMaterialService.stockCount(dto));
    }


    @GetMapping(value = "/material-history-page")
    @ApiOperation("产品历史明细列表分页")
    public R getMaterialHistoryPage(MaterialHistoryPageDto dto) {
        return R.ok(caseErpMaterialService.getMaterialHistoryPage(dto));
    }

}
